<template>
  <MesBox title="生成采购单" v-model="visible" :width="800" :height="300">
    <MesForm
      ref="form"
      :formFields="formFields"
      :formRules="formRules"
      v-if="formRules.length"
    ></MesForm>
    <template #footer>
      <el-button type="danger" @click="save">
        <i class="el-icon-check"></i>
        保存</el-button
      >
    </template>
  </MesBox>
</template>

<script setup>
import { getCurrentInstance, ref, defineEmits } from 'vue'
import MesBox from '@/components/basic/MesBox.vue'
import MesForm from '@/components/basic/MesForm.vue'

defineEmits(['parentCall'])
const that = getCurrentInstance().appContext.config.globalProperties
const form = ref(null)
let visible = ref(false)
let formFields = ref({
  Supplier: '',
  Qty: 0,
  DeliveryDate: ''
})
let stype = ''
let id = ''
let formRules = []

let open = (row, type, formrule) => {
  Object.assign(formRules, formrule)
  // formRules = formrule
  formRules[2][1].max = row.PlanQty
  for (let key in row) {
    if (key == 'PlanQty') {
      formFields.value.Qty = row[key]
    } else {
      formFields.value[key] = row[key]
    }
  }
  stype = type
  id = type == '工单' ? row.WorkOrderChildren_Id : row.WorkOrderList_Id
  visible.value = true
  // console.log(id);
}

let save = () => {
  form.value.validate(async (valid) => {
    if (valid) {
      let params = {
        id,
        type: stype,
        ...formFields.value,
      }
      let res = await that.http.post(
        '/api/Production_OSPurchaseOrder/CreateOSPurchase',
        params,
        true
      )
      if (res.status) {
        that.$message.success(res.message)
        visible.value = false
      } else {
        that.$message.error(res.message)
      }
    }
  })
}
defineExpose({ open })
</script>

<style lang="less" scoped></style>
